# 基于官方Python镜像，添加Java, Maven和Allure
FROM python:3.9-slim

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
ENV MAVEN_HOME=/opt/maven
ENV ALLURE_HOME=/opt/allure
ENV PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:$ALLURE_HOME/bin

# 设置工作目录
WORKDIR /app

# 安装系统依赖
RUN apt-get update && apt-get install -y \
    # Java 11 (OpenJDK)
    openjdk-11-jdk \
    # 下载工具
    wget \
    curl \
    unzip \
    # 构建工具
    gcc \
    default-libmysqlclient-dev \
    pkg-config \
    git \
    # Allure依赖
    fontconfig \
    && rm -rf /var/lib/apt/lists/*

# 安装Maven 3.9.4
RUN wget https://archive.apache.org/dist/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz -O /tmp/maven.tar.gz && \
    tar -xzf /tmp/maven.tar.gz -C /opt && \
    mv /opt/apache-maven-3.9.4 /opt/maven && \
    rm /tmp/maven.tar.gz

# 安装Allure Commandline
RUN wget https://github.com/allure-framework/allure2/releases/download/2.24.0/allure-2.24.0.tgz -O /tmp/allure.tgz && \
    tar -xzf /tmp/allure.tgz -C /opt && \
    mv /opt/allure-2.24.0 /opt/allure && \
    rm /tmp/allure.tgz && \
    ln -s /opt/allure/bin/allure /usr/bin/allure

# 升级pip
RUN pip install --upgrade pip

# 复制依赖文件
COPY requirements.txt .

# 安装Python依赖
RUN pip install --no-cache-dir -r requirements.txt

# 安装Allure Pytest插件
RUN pip install allure-pytest

# 复制项目文件
COPY . .

# 创建非root用户
RUN groupadd -r appuser && useradd -r -g appuser appuser
RUN chown -R appuser:appuser /app
USER appuser

# 暴露端口
EXPOSE 5000

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:5000/health || exit 1

# 启动命令
CMD ["python", "run.py"]